package com.noyes4j.device.controller;

import com.noyes4j.common.core.controller.BaseController;
import com.noyes4j.common.core.domain.R;
import com.noyes4j.common.core.page.TableDataInfo;
import com.noyes4j.common.log.annotation.OperLog;
import com.noyes4j.common.log.enums.BusinessType;
import com.noyes4j.common.utils.poi.ExcelUtil;
import com.noyes4j.device.domain.SysDevice;
import com.noyes4j.device.service.ISysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SysDeviceController
 * @Description: TODO
 * @Author zhaoweiqin
 * @Date 2021/1/29
 * @Version V1.0
 **/

@Controller
@RequestMapping("/ota/device")
public class SysDeviceController  extends BaseController
{
    private String prefix = "ota/device";

    @Autowired
    private ISysDeviceService sysDeviceService;

    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDevice sysDevice)
    {
        startPage();
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PostMapping("/export")
    @ResponseBody
    public R export(SysDevice sysDevice)
    {
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        return util.exportExcel(list, "device");
    }

    /**
     * 新增设备
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备
     */
    @OperLog(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public R addSave(SysDevice sysDevice)
    {
        return toAjax(sysDeviceService.insertSysDevice(sysDevice));
    }

    /**
     * 修改设备
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysDevice sysDevice = sysDeviceService.selectSysDeviceById(id);
        mmap.put("sysDevice", sysDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备
     */
    @OperLog(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public R editSave(SysDevice sysDevice)
    {
        return toAjax(sysDeviceService.updateSysDevice(sysDevice));
    }

    /**
     * 删除设备
     */
    @OperLog(title = "设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public R remove(String ids)
    {
        return toAjax(sysDeviceService.deleteSysDeviceByIds(ids));
    }
}